package com.gemptc.yueban.service;

import java.io.IOException;
import java.io.PrintWriter;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.gemptc.bean.ActivityBean;
import com.gemptc.bean.RegistrationBean;
import com.gemptc.wode.daoImp.Select_WouserImp;
import com.gemptc.yueban.daoImp.SelectActivityImp;
import com.gemptc.yueban.daoImp.SelectRegistrationImp;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

/**
 * Servlet implementation class YueBan_Middle
 */
@WebServlet("/YueBan_Middle")
public class YueBan_Middle extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public YueBan_Middle() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		PrintWriter printWriter = response.getWriter();
		String Value = request.getParameter("key");
		Select_WouserImp wouser = new Select_WouserImp();
		String username;
		int userid;
		System.out.println(Value + "---------->");
		switch (Value) {
		case "1":
			List<ActivityBean> list1 = new ArrayList<ActivityBean>();
			SelectActivityImp s1 = new SelectActivityImp();
			String getall = request.getParameter("all");
			list1 = s1.getAllBean(Integer.valueOf(getall));
			Gson gson1 = new Gson();
			Type type = new TypeToken<List<ActivityBean>>() {
			}.getType();
			String all = gson1.toJson(list1, type);
			printWriter.write(all);
			break;
		case "2":
			List<ActivityBean> list2 = new ArrayList<ActivityBean>();
			SelectActivityImp s2 = new SelectActivityImp();
			String gethot = request.getParameter("hot");
			list2 = s2.getHotBean(Integer.valueOf(gethot));
			Gson gson2 = new Gson();
			Type type2 = new TypeToken<List<ActivityBean>>() {
			}.getType();
			String hot = gson2.toJson(list2, type2);
			printWriter.write(hot);
			break;
		case "3":
			List<ActivityBean> list3 = new ArrayList<ActivityBean>();
			SelectActivityImp s3 = new SelectActivityImp();
			String getweek = request.getParameter("week");
			System.out.println(getweek);
			list3 = s3.getWeekBean(Integer.valueOf(getweek));
			Gson gson3 = new Gson();
			Type type3 = new TypeToken<List<ActivityBean>>() {
			}.getType();
			String week = gson3.toJson(list3, type3);
			printWriter.write(week);
			break;
		case "4":
			List<ActivityBean> list4 = new ArrayList<ActivityBean>();
			SelectActivityImp s4 = new SelectActivityImp();
			String getPeople = request.getParameter("people");
			list4 = s4.getPeopleBean(Integer.valueOf(getPeople));
			Gson gson4 = new Gson();
			Type type4 = new TypeToken<List<ActivityBean>>() {
			}.getType();
			String people = gson4.toJson(list4, type4);
			printWriter.write(people);
			break;
		case "5":
			List<RegistrationBean> list5 = new ArrayList<RegistrationBean>();
			SelectRegistrationImp s5 = new SelectRegistrationImp();
			String getAc_id = request.getParameter("ac_id");
			list5 = s5.getRegistrationBean(Integer.parseInt(getAc_id));
			Gson gson5 = new Gson();
			Type type5 = new TypeToken<List<RegistrationBean>>() {
			}.getType();
			String re = gson5.toJson(list5, type5);
			printWriter.write(re);
			break;
		case "6":

			List<ActivityBean> list11 = new ArrayList<ActivityBean>();
			SelectActivityImp s11 = new SelectActivityImp();
			username = request.getParameter("username");
			userid = wouser.selectuserid(username);
			list11 = s11.getAll(userid);
			Gson gson11 = new Gson();
			Type type1 = new TypeToken<List<ActivityBean>>() {
			}.getType();

			String all1 = gson11.toJson(list11, type1);
			printWriter.write(all1);

			break;
		case "7":
			List<ActivityBean> ablist = new ArrayList<ActivityBean>();
			SelectActivityImp sai = new SelectActivityImp();
			username = request.getParameter("username");
			userid = wouser.selectuserid(username);
			ablist = sai.getwobaoming(userid);
			Gson gson = new Gson();
			Type tp = new TypeToken<List<ActivityBean>>() {
			}.getType();
			String re1 = gson.toJson(ablist, tp);
			printWriter.write(re1);

			break;
		case "8":
			SelectRegistrationImp selectsum=new SelectRegistrationImp();
			String ac_id=request.getParameter("ac_id");
			int ac_num_sum=selectsum.getSum(Integer.parseInt(ac_id));			
			printWriter.write(String.valueOf(ac_num_sum));
			break;
		case "9":

			String ac_id1=request.getParameter("ac_id");
			String user_id1=request.getParameter("user_id");
			System.out.println(ac_id1);
			System.out.println(user_id1);
			SelectActivityImp sa = new SelectActivityImp();
			
			boolean fg=sa.deletbaoming(Integer.parseInt(user_id1), Integer.parseInt(ac_id1));
			System.out.println(fg);
			if (fg) {
				printWriter.write("succcess");
			}else{
				printWriter.write("erro");
			}
			break;
		default:
			break;
		}
	}

}
